package com.hc.dfs.anim;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.MotionEvent;
import android.widget.Toast;

/** 
 * @author 王洪胜  E-mail:  
 * @version 创建时间：2013-2-21 上午8:56:02
 * 
 * 类说明：常用上下左右滑动操作类
 *
 */
public class CommonGesture extends BaseGesture {

	//手势滑动最小生效距离
	private float fling_min_distance;
	//当前Activity的上下文
	private Context fling_context;
	//右滑动要启动的Activity是上一个Activity，传递一个标志位表示是否到边界（0表示到边界，非0数字表示未到边界）
	private int fling_r_cls;
	//右滑动Activity进入动画
	private int fling_r_anim_in;
	//右滑动Activity进入动画
	private int fling_r_anim_out;
	//左滑动要启动的Activity的Class
	private Class<?> fling_l_cls;
	//左滑动Activity进入动画
	private int fling_l_anim_in;
	//左滑动Activity退出动画
	private int fling_l_anim_out;
	
	//构造方法
	public CommonGesture(
			float min_distance, 
			Context fling_context, 
			Class<?> fling_l_cls,
			int fling_l_anim_in,
			int fling_l_anim_out,
			int fling_r_cls,
			int fling_r_anim_in,
			int fling_r_anim_out
	){
		this.fling_min_distance = min_distance;
		this.fling_context = fling_context;
		this.fling_l_cls = fling_l_cls;
		this.fling_l_anim_in = fling_l_anim_in;
		this.fling_l_anim_out = fling_l_anim_out;
		this.fling_r_cls = fling_r_cls;
		this.fling_r_anim_in = fling_r_anim_in;
		this.fling_r_anim_out = fling_r_anim_out;
	}
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
		float eX = 0;
		float eY = 0;
		// e1：第1个ACTION_DOWN MotionEvent 
		// e2：最后一个ACTION_MOVE MotionEvent 
		// velocityX：X轴上的移动速度，像素/秒 
		// velocityY：Y轴上的移动速度，像素/秒
		if(e1 instanceof MotionEvent){
			//防止e1不存在造成空指针错误
			eX = e2.getX()-e1.getX();
			eY = e2.getY()-e1.getY();
		}
		if(Math.abs(eX)-Math.abs(eY)>0){
			//横向滑动
			if((eX<-fling_min_distance)){
				//左滑动
				System.out.println("横向向左滑动");
				if(fling_l_cls!=null){
					Intent intent = new Intent(fling_context, fling_l_cls);
					fling_context.startActivity(intent);
					((Activity)fling_context).overridePendingTransition(fling_l_anim_in, fling_l_anim_out);
				}else{
					Toast.makeText(fling_context, "兄弟，后面没东西啦", Toast.LENGTH_SHORT).show();
				}
			}else if(eX>fling_min_distance){
				if(fling_r_cls!=0){
					//右滑动，销毁当前Activity
					System.out.println("横向向右滑动");
//					Intent intent = new Intent(fling_context, fling_r_cls);
//					fling_context.startActivity(intent);
					((Activity)fling_context).finish();
					((Activity)fling_context).overridePendingTransition(fling_r_anim_in, fling_r_anim_out);
				}else{
					Toast.makeText(fling_context, "哥们，到最前面啦", Toast.LENGTH_SHORT).show();
				}
			}
		}else if(Math.abs(eX)-Math.abs(eY)<0){
			//纵向滑动
			if(eY>fling_min_distance){
				//下滑动
				System.out.println("纵向向下滑动");
			}else if(eY<-fling_min_distance){
				//上滑动
				System.out.println("纵向向上滑动");
			}
		}else{
			//未滑动或45°向右下角滑动
		}
		return true;
	}

}
